//
//  WYFilePathHeiper.m
//  test
//
//  Created by longtu on 15/4/21.
//  Copyright (c) 2015年 longtu. All rights reserved.
//

#import "WYDevHelper.h"

@implementation WYFilePathHelper

#define BT (1024)
#define KB (BT*1024)
#define MB (KB*1024)
#define GB (MB*1024)

#define MINUTE (60)
#define HOUR (MINUTE*60)

// DocumentDirectory
+ (NSString *) fileDocumentDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

// LibraryDirectory
+ (NSString *) fileLibraryDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}

// CachesDirectory
+ (NSString*) fileCachesDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

// 单个文件的大小
+ (float ) fileSizeAtPath:(NSString*) filePath{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSError *error=nil;
    NSDictionary *ret=[fileManager attributesOfItemAtPath:filePath error:&error];
    if(error){
        NSLog(@"getFileSizeAtPath===============%@",error);
    }
    return [[ret objectForKey:NSFileSize] doubleValue];
}

// 遍历文件夹获得文件夹大小，返回多少M
+ (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath])
        return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    
    return folderSize/KB;
}

// 传入数字返回文件大小的字符串
+ (NSString *) transferNumberToString:(float )number{
    NSString *result=@"";
    if(number < BT){
        // byte
        result=[NSString stringWithFormat:@"%0.2f bytes",number];
    }else if(number<KB){
        // kB
        result=[NSString stringWithFormat:@"%0.2f KB",number/BT];
    }else if(number<MB){
        // MB
        result=[NSString stringWithFormat:@"%0.2f MB",number/BT/BT];
    }else{
        // GB
        result=[NSString stringWithFormat:@"%0.2f GB",number/BT/BT/BT];
    }
    return result;
}

// 传入数字返回时间的字符串
+ (NSString *) formatTimeByNumber:(float)time{
    NSString *result=@"00:00";
    //hour
    if(time >= HOUR){
        int hour = time / HOUR;
        int minute = (time - hour * HOUR) / MINUTE;
        int second = time - hour * HOUR - minute * MINUTE;
        
        NSString *hourString = [NSString stringWithFormat:@"%d",hour];
        if(hour<10){
            hourString = [NSString stringWithFormat:@"0%d",hour];
        }
        NSString *minString =[ NSString stringWithFormat:@"%d",minute];
        if(minute<10){
            minString = [NSString stringWithFormat:@"0%d",minute];
        }
        NSString *secondString = [NSString stringWithFormat:@"%d",second];
        if(second<10){
            secondString = [NSString stringWithFormat:@"0%d",second];
        }
        result = [NSString stringWithFormat:@"%@:%@:%@",hourString,minString,secondString];
        
    }else if(time < HOUR){
        int minute = time / MINUTE;
        int second = time - minute * MINUTE;;
        NSString *minString = [NSString stringWithFormat:@"%d",minute];
        if(minute < 10){
            minString = [NSString stringWithFormat:@"0%d",minute];
        }
        NSString *secondString = [NSString stringWithFormat:@"%d",second];
        if(second < 10){
            secondString = [NSString stringWithFormat:@"0%d",second];
        }
        result = [NSString stringWithFormat:@"%@:%@",minString,secondString];
    }
    return result;
}

// 是否存在指定文件
+ (BOOL) isExistFileAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:path];
}

// 删除path文件
+ (BOOL) removeFileAtPath:(NSString *)path{
    NSError *error=nil;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:path]){
        return NO;
    }
    BOOL result= [fileManager removeItemAtPath:path error:&error];
    if(error){
        NSLog(@"移除文件失败：%@",error);
        result=NO;
    }
    return result;
}


/**
 *    @param     sourcePath     Resource文件路径
 *    @param     toPath     把文件拷贝到XXX文件夹
 *    @return    BOOL
 */
+ (BOOL) copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath{
    BOOL retVal = YES; // If the file already exists, we'll return success…
    NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation]){
        retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
    }
    return retVal;
}

/**
 *    @brief    创建文件夹
 *    @param     createDir     创建文件夹路径
 */
+ (void) createFolder:(NSString *)createPath{
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:createPath isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) ){
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}


@end
